package com.itheima.vmmysql.Controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.vmmysql.Config.DataBaseConfig;
import com.itheima.vmmysql.Service.DataBaseTestService;

@RestController
@CrossOrigin(origins = "*")


@RequestMapping("/api")
public class DabaBaseTestController {

    private DataBaseTestService dataBaseTestService;
         public void DatabaseController(DataBaseTestService dataBaseTestService) {
            this.dataBaseTestService = dataBaseTestService;
    }

    @PostMapping("/test-connection")
    public ResponseEntity<Map<String, String>> testConnection(@RequestBody DataBaseConfig config) {
        boolean isConnected = dataBaseTestService.testConnection(config);
        Map<String, String> response = new HashMap<>();
        response.put("message", isConnected ? "连接成功！" : "连接失败，请检查配置。");
        return ResponseEntity.ok(response);
    }
}